/**
 * Author: Elysia Haight
 * 
 * Class for the medical profile activity
 */

package com.dietdroid;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MedicalProfileActivity extends Activity {

    private ProfileData profile;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.medical_profile_activity);
        
        
        Button b = (Button)findViewById(R.id.allergenScreen);
        b.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
    			Intent a = new Intent(MedicalProfileActivity.this, AllergenActivity.class);
    			startActivity(a);
            }
        });
        
        profile = DataStorage.getProfileData(this.getApplicationContext());
        fillView();
    }

    public void onPause() {
        super.onPause();

        updateProfile();
        DataStorage.saveProfileData(this.getApplicationContext(), profile);
    }

    public void onStop() {
        super.onStop();
        updateProfile();
        DataStorage.saveProfileData(this.getApplicationContext(), profile);
    }

    public void onDestroy() {
        super.onDestroy();
        updateProfile();
        DataStorage.saveProfileData(this.getApplicationContext(), profile);
    }

    private void fillView() {
        populateViewItem(R.id.caloriesMaximumInput, profile.getMaxCals());
        populateViewItem(R.id.fatMaximumInput, profile.getMaxFat());
        populateViewItem(R.id.cholMaximumInput, profile.getMaxChol());
        populateViewItem(R.id.sugarMaximumInput, profile.getMaxSugar());
        populateViewItem(R.id.carbMaximumInput, profile.getMaxCarbs());
        populateViewItem(R.id.proteinMaximumInput, profile.getMaxProtein());
        populateViewItem(R.id.sodiumMaximumInput, profile.getMaxSodium());
        
    }

    private void updateProfile() {
        profile.setMaxCals(retrieveViewData(R.id.caloriesMaximumInput));
        profile.setMaxFat(retrieveViewData(R.id.fatMaximumInput));
        profile.setMaxChol(retrieveViewData(R.id.cholMaximumInput));
        profile.setMaxSodium(retrieveViewData(R.id.sugarMaximumInput));
        profile.setMaxCarbs(retrieveViewData(R.id.carbMaximumInput));
        profile.setMaxProtein(retrieveViewData(R.id.proteinMaximumInput));
        profile.setMaxSodium(retrieveViewData(R.id.sodiumMaximumInput));
        
    }

    private int retrieveViewData(int viewId) {
        EditText textItem = (EditText) findViewById(viewId);
        int retInt = 0;

        if (textItem != null && !textItem.getText().toString().equals("")) {
            retInt = Integer.parseInt(textItem.getText().toString());
        }
        return retInt;
    }

    private void populateViewItem(int viewId, int viewData) {
        EditText textItem = (EditText) findViewById(viewId);
        if (textItem != null) {
            textItem.setText(String.valueOf(viewData), TextView.BufferType.EDITABLE);
        }
    }
    

//    
//    private class RemoveClickListener implements View.OnClickListener
//    {
//
//		@Override
//		public void onClick(View v) {
//			final ListView allergenList = (ListView) findViewById(R.id.allergenList);
//			final int index = allergenList.getSelectedItemPosition();
//			
//			if(index != -1)
//			{
//				AlertDialog.Builder removeConfirm = new AlertDialog.Builder(MedicalProfileActivity.this);
//				removeConfirm.setCancelable(true);
//				removeConfirm.setMessage("Are you sure you want to remove this allergen?");
//				removeConfirm.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
//					
//					@Override
//					public void onClick(DialogInterface dialog, int which) {
//						dialog.dismiss();
//						profile.removeAllergen(index);
//			            
//			            ArrayAdapter<String> adapter = (ArrayAdapter<String>) allergenList.getAdapter();
//			            adapter.notifyDataSetChanged();
//					}
//				});
//				
//				removeConfirm.setNegativeButton("No", new DialogInterface.OnClickListener(){
//					@Override
//					public void onClick(DialogInterface dialog, int which) {
//						dialog.dismiss();
//					}
//				});
//				
//				removeConfirm.show();
//				
//			}
//		}
//    	
//    }
//   


}
